#load data
colnames<-c("price","meters","price_pre","first_pay","month_pay","region","orientation","floor","name","coordy","coordx","link","title")
data<-read.table("./data/lianjia-bj-ershoufan.csv",header=F,sep=",",col.names=colnames)
#remove "平米“
data$meters<-sub("[^0-9]+","",data$meters)
#remove " 元/m²"
data$price_pre<-sub("[^0-9]+","",data$price_pre)
#remove "万"
data$price<-sub("[^0-9]+","",data$price)
#convert to integer
data$price<-as.integer(data$price)
data$meters<-as.integer(data$meters)
data$price_pre<-as.integer(data$price_pre)

print(paste("data =",dim(data),sep=""))

#load coordinate file
#colnames<-c("name","coordx","coordy")
#coord <- read.table("./data/lianjia-bj-name-coordinate.csv",header=F,sep=",",col.names=colnames)

# MODEL_NAME
#data <- merge(data, coord, by="name")
#dim(data)

#remove no coordinate items
data <- data[which(data$coordx != 0 & data$coordy != 0),]
print(dim(data))
#filter meters from 20 ~ 200 pingmi
data <- data[which(data$meters >20 & data$meters <= 200),]
print(dim(data))
#filter price_pre from 20 ~ 200 w
data <- data[which(data$price_pre <= 35000),]
print(dim(data))

#remove rows by keyword
print("remove rows by keyword")
t<-grep("地下",data$name)
if (length(t) != 0)
	data <- data[-t,]
print(dim(data))
t<-grep("地下",data$region)
if (length(t) != 0)
	data <- data[-t,]
print(dim(data))


t<-grep("燕郊",data$name)
if (length(t) != 0)
	data <- data[-t,]
print(dim(data))
t<-grep("燕郊",data$region)
if (length(t) != 0)
	data <- data[-t,]
print(dim(data))

t<-grep("地下",data$name)
if (length(t) != 0)
	data <- data[-t,]
print(dim(data))
t<-grep("地下",data$region)
if (length(t) != 0)
	data <- data[-t,]
print(dim(data))

t<-grep("地下",data$floor)
if (length(t) != 0)
	data <- data[-t,]
print(dim(data))

#ring data
ring0<-c(topleftX=116.398175,topleftY=39.928832,downrightX=116.40838,downrightY=39.919536)
ring2<-c(topleftX=116.362961,topleftY=39.953947,downrightX=116.441725,downrightY=39.908799)
ring3<-c(topleftX=116.313519,topleftY=39.969874,downrightX=116.461272,downrightY=39.864508)
ring35<-c(topleftX=116.30202,topleftY=39.977836,downrightX=116.473345,downrightY=39.857862)
ring4<-c(topleftX=116.281898,topleftY=39.989335,downrightX=116.488293,downrightY=39.839249)
ring45<-c(topleftX=116.2727,topleftY=39.998622,downrightX=116.507265,downrightY=39.823292)
ring5<-c(topleftX=116.223257,topleftY=40.020285,downrightX=116.546934,downrightY=39.806443)
ring55<-c(topleftX=116.158292,topleftY=40.0658,downrightX=116.554408,downrightY=39.781606)
ring6<-c(topleftX=116.114023,topleftY=40.172176,downrightX=116.67629,downrightY=39.691053)

#filter from ringa to ringb
filterbyring<-function(data,ring_inside,ring_outside)
{
	d<-data[which(data$coordx > ring_outside["topleftX"] & data$coordy < ring_outside["topleftY"] &
				  data$coordx < ring_outside["downrightX"] & data$coordy > ring_outside["downrightY"] ),]
	d2<-d[which(!(d$coordx > ring_inside["topleftX"] & d$coordy < ring_inside["topleftY"] &
				  d$coordx < ring_inside["downrightX"] & d$coordy > ring_inside["downrightY"] )),]
	return(d2)			  
}

#filter

d0 <- filterbyring(data, ring0, ring2)
d1 <- filterbyring(data, ring2, ring3)
d2 <- filterbyring(data, ring3, ring35)
d3 <- filterbyring(data, ring35, ring4)
d4 <- filterbyring(data, ring4, ring45)
d5 <- filterbyring(data, ring45, ring5)
d6 <- filterbyring(data, ring5, ring55)
d7 <- filterbyring(data, ring55, ring6)

print("*****************1")
print(dim(d0))
print(dim(d1))
print(dim(d2))
print(dim(d3))
print(dim(d4))
print(dim(d5))
print(dim(d6))
print(dim(d7))
print("*****************2")

#sort
#result1<-result1[order(result1$price_pre),]

#draw  

####################################################3


png(filename="./price-meters.png",width = 1024, height = 2048)
split.screen(c(4,2))
screen(1)

plot(d0$price,d0$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(2)
plot(d1$price,d1$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(3)
plot(d2$price,d2$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(4)
plot(d3$price,d3$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(5)
plot(d4$price,d4$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(6)
plot(d5$price,d5$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(7)
plot(d6$price,d6$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(8)
plot(d7$price,d7$meters,lty=3,xlim=c(0,1000),ylim=c(0,200))
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)

dev.off()



####################################################3


png(filename="./price-meters-30-100.png",width = 1024, height = 2048)
split.screen(c(4,2))
screen(1)

plot(d0$price,d0$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="0~2 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(2)
plot(d1$price,d1$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="2~3 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(3)
plot(d2$price,d2$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="3~3.5 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(4)
plot(d3$price,d3$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="3.5~4 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(5)
plot(d4$price,d4$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="4~4.5 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(6)
plot(d5$price,d5$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="4.5~5 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(7)
plot(d6$price,d6$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="5~5.5 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)
screen(8)
plot(d7$price,d7$meters,lty=3,xlim=c(30,100),ylim=c(0,200),main="5.5~6 ring")
abline(c(0,0),0.33)
abline(c(0,0),0.5)
abline(c(0,0),1)

dev.off()